import os, sys
from cStringIO import StringIO
from settings import SETTINGS
from subprocess import call

def separator():
    if sys.platform == "win32": 
	    return ";" 
    else:
        return ":"

def start_feed():    
    jar_files = get_filtered_files(SETTINGS['classpath'], ".jar")    
    classpath = create_classpath(jar_files)    
    classpath += separator() + SETTINGS["config_path"]
    
    os.system("java -cp %s Main" % classpath)
    retcode = call("java -cp %s Main" % classpath, shell=True)
    sys.exit(retcode)
	
def create_classpath(file_names):
    try:
        string_buffer = StringIO()
        for file_name in file_names:
            string_buffer.write("%s%s" % (file_name, separator()))
        return string_buffer.getvalue()
    finally:
        string_buffer.close()

def get_filtered_files(dir, extension):
    lib_files = map(lambda each: dir + "/%s" % each, os.listdir(dir))
    return filter(lambda f: f.endswith(extension), lib_files)
	
if __name__ == "__main__":
    start_feed()
